package pl.gwt.client.gui;

import java.util.Iterator;
import java.util.List;

import pl.gwt.client.ClientDetailsService;
import pl.gwt.client.ClientDetailsServiceAsync;
import pl.gwt.client.common.Client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.MenuItem;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;
import com.google.gwt.user.client.ui.VerticalPanel;


public class RightPanelClientDetails extends RightPanel {
	DockPanel workPanel = new DockPanel();
	VerticalPanel detailsPanel = new VerticalPanel();
	MenuBar menu = new MenuBar();
	MenuBar clientMenu = new MenuBar(true);
	MenuBar projectsMenu = new MenuBar(true);
	MenuBar calendarMenu = new MenuBar(true);
	Tree projectsTree = new Tree();
	
	boolean exist = true;
	
	//client details
	Label clientNameLabel = new Label("");
	Label clientPhoneNoLabel = new Label("");
	Label clientAddressLabel = new Label("");
	Label clientCountryLabel = new Label("");
	Label clientCityLabel = new Label("");
	Label clientStateLabel = new Label("");
	
	String clientName = "";
	
	final ClientDetailsServiceAsync getClientDetailsService = (ClientDetailsServiceAsync) GWT
	.create(ClientDetailsService.class);
	
	public RightPanelClientDetails(String clientName){		
		super("CLIENT DETAILS");
		
		initMenuBar();
		
		this.clientName = clientName;
		this.clientNameLabel.setText(clientName);
		this.clientNameLabel.setStyleName("bold");
		initDetails();
		initProjectsTree();
		
		detailsPanel.add(clientNameLabel);
		detailsPanel.add(clientPhoneNoLabel);
		detailsPanel.add(clientAddressLabel);
		detailsPanel.add(clientCountryLabel);
		detailsPanel.add(clientCityLabel);
		detailsPanel.add(clientStateLabel);
		
		workPanel.add(menu, DockPanel.NORTH);
		workPanel.add(detailsPanel, DockPanel.WEST);
		workPanel.add(projectsTree, DockPanel.EAST);
		workPanel.setStyleName("gwt-DockPanel");
		
		addInsidePanel(workPanel);
	}
	
	private void initProjectsTree(){
		ServiceDefTarget endPointClientDetails = (ServiceDefTarget) getClientDetailsService;
		endPointClientDetails.setServiceEntryPoint("pl.gwt.server");
		
		AsyncCallback<List<String>> projectsCallback = new AsyncCallback<List<String>>(){

			public void onFailure(Throwable caught) {
				
			}

			public void onSuccess(List<String> result) {
				TreeItem projects = new TreeItem("Projects");
				
				projectsTree.addItem(projects);
				for(Iterator<String> iter = result.iterator();; iter.hasNext()){
					projects.addItem(iter.next());
				}
				
			}
		};
		
		getClientDetailsService.getProjects(clientName, projectsCallback);
	}
	
	private void initMenuBar(){
		menu.setAutoOpen(true);
	    menu.setWidth("500px");
	    menu.setAnimationEnabled(true);
	    
	    clientMenu.setAnimationEnabled(true);
	    menu.addItem(new MenuItem("Client", clientMenu));
	    projectsMenu.setAnimationEnabled(true);
	    menu.addItem(new MenuItem("Projects", projectsMenu));
	    calendarMenu.setAnimationEnabled(true);
	    menu.addItem(new MenuItem("Calendar", calendarMenu));
	    
	    //add new client to DB
	    clientMenu.addItem("Add New Client", new Command(){
			public void execute() {

			}	
	    });
	    
	    //edit client details
	    clientMenu.addItem("Edit Current Client", new Command(){

			public void execute() {
				
			}
	    
	    });
	    
	    //remove client from DB
	    clientMenu.addItem("Remove Current Client", new Command(){

			public void execute() {
				
			}
	    	
	    });
	    
	    //create new project for current selected customer
	    projectsMenu.addItem("Create new project for current customer", new Command(){
			public void execute() {
				
			}
	    });
	    
	    //add new client task
	    calendarMenu.addItem("Add client task", new Command(){
			public void execute() {
				
			}
	    });
	    
	    //add establishment from last contact with a current selected client
	    calendarMenu.addItem("Add new contact details", new Command(){
			public void execute() {
				
			}	    	
	    });
	}
	
	private void initDetails() {
		ServiceDefTarget endPointClientDetails = (ServiceDefTarget) getClientDetailsService;
		endPointClientDetails.setServiceEntryPoint("pl.gwt.server");

		// get the phone number
		AsyncCallback<String> callbackPhoneNo = new AsyncCallback<String>() {
			public void onFailure(Throwable caught) {
				exist = false;
			}

			public void onSuccess(String result) {
				clientPhoneNoLabel.setText(result);
			}
		};

		
		// get the address
		AsyncCallback<String> callbackAddress = new AsyncCallback<String>() {
			public void onFailure(Throwable caught) {
				exist = false;
			}

			public void onSuccess(String result) {
				clientAddressLabel.setText(result);
			}
		};

		// get the country
		AsyncCallback<String> callbackCountry = new AsyncCallback<String>() {
			public void onFailure(Throwable caught) {
				exist = false;
			}

			public void onSuccess(String result) {
				clientCountryLabel.setText(result);
			}
		};

		// get the city
		AsyncCallback<String> callbackCity = new AsyncCallback<String>() {
			public void onFailure(Throwable caught) {
				exist = false;
			}

			public void onSuccess(String result) {
				clientCityLabel.setText(result);
			}
		};

		// get the state
		AsyncCallback<String> callbackState = new AsyncCallback<String>() {
			public void onFailure(Throwable caught) {
				exist = false;
			}

			public void onSuccess(String result) {
				clientStateLabel.setText(result);
			}
		};
		// get the phone number
		AsyncCallback<Client> callbackClient = new AsyncCallback<Client>() {
			public void onFailure(Throwable caught) {
				exist = false;
			}

			public void onSuccess(Client client) {
				if(client.isInit()){
					clientPhoneNoLabel.setText(client.getPhone_no());
					//TODO: czy state jest potrzebny?
					clientStateLabel.setText("Malopolskie");
					clientCityLabel.setText(client.getCity());
					clientCountryLabel.setText(client.getCountry());
					clientAddressLabel.setText(client.getAddress());
					clientPhoneNoLabel.setText(client.getPhone_no());
				}
				else{
					final DialogBox dialogBox = new DialogBox();
					dialogBox.setText("Remote Procedure Call");
					dialogBox.setAnimationEnabled(true);
					final Button closeButton = new Button("Close");
					// We can set the id of a widget by accessing its Element
					//closeButton.getElement().setId("closeButton");
					VerticalPanel dialogVPanel = new VerticalPanel();

					dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
					dialogVPanel.add(closeButton);
					
					dialogBox.setWidget(dialogVPanel);

					// Add a handler to close the DialogBox
					closeButton.addClickHandler(new ClickHandler() {
						public void onClick(ClickEvent event) {
							dialogBox.hide();							
						}
					});
					
					dialogBox.setText("Wrong client name");				
					dialogBox.center();
					closeButton.setFocus(true);					
					
				}
					
			}
		};
		getClientDetailsService.getClient(clientName, callbackClient);
		
	}

}



/*package pl.gwt.client.gui;

import java.util.Iterator;
import java.util.List;

import pl.gwt.client.ClientDetailsService;
import pl.gwt.client.ClientDetailsServiceAsync;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.MenuItem;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;
import com.google.gwt.user.client.ui.VerticalPanel;


public class RightPanelClientDetails extends RightPanel {
	DockPanel workPanel = new DockPanel();
	VerticalPanel detailsPanel = new VerticalPanel();
	MenuBar menu = new MenuBar();
	MenuBar clientMenu = new MenuBar(true);
	MenuBar projectsMenu = new MenuBar(true);
	MenuBar calendarMenu = new MenuBar(true);
	Tree projectsTree = new Tree();
	
	boolean exist = true;
	
	//client details
	Label clientNameLabel = new Label("");
	Label clientPhoneNoLabel = new Label("");
	Label clientAddressLabel = new Label("");
	Label clientCountryLabel = new Label("");
	Label clientCityLabel = new Label("");
	Label clientStateLabel = new Label("");
	
	String clientName = "";
	
	final ClientDetailsServiceAsync getClientDetailsService = (ClientDetailsServiceAsync) GWT
	.create(ClientDetailsService.class);
	
	public RightPanelClientDetails(String clientName){		
		super("CLIENT DETAILS");
		
		initMenuBar();
		
		this.clientName = clientName;
		this.clientNameLabel.setText(clientName);
		this.clientNameLabel.setStyleName("bold");
		initDetails();
		initProjectsTree();
		
		detailsPanel.add(clientNameLabel);
		detailsPanel.add(clientPhoneNoLabel);
		detailsPanel.add(clientAddressLabel);
		detailsPanel.add(clientCountryLabel);
		detailsPanel.add(clientCityLabel);
		detailsPanel.add(clientStateLabel);
		
		workPanel.add(menu, DockPanel.NORTH);
		workPanel.add(detailsPanel, DockPanel.WEST);
		workPanel.add(projectsTree, DockPanel.EAST);
		workPanel.setStyleName("gwt-DockPanel");
		
		addInsidePanel(workPanel);
	}
	
	private void initProjectsTree(){
		ServiceDefTarget endPointClientDetails = (ServiceDefTarget) getClientDetailsService;
		endPointClientDetails.setServiceEntryPoint("pl.gwt.server");
		
		AsyncCallback<List<String>> projectsCallback = new AsyncCallback<List<String>>(){

			public void onFailure(Throwable caught) {
				
			}

			public void onSuccess(List<String> result) {
				TreeItem projects = new TreeItem("Projects");
				
				projectsTree.addItem(projects);
				for(Iterator<String> iter = result.iterator();; iter.hasNext()){
					projects.addItem(iter.next());
				}
				
			}
		};
		
		getClientDetailsService.getProjects(clientName, projectsCallback);
	}
	
	private void initMenuBar(){
		menu.setAutoOpen(true);
	    menu.setWidth("500px");
	    menu.setAnimationEnabled(true);
	    
	    clientMenu.setAnimationEnabled(true);
	    menu.addItem(new MenuItem("Client", clientMenu));
	    projectsMenu.setAnimationEnabled(true);
	    menu.addItem(new MenuItem("Projects", projectsMenu));
	    calendarMenu.setAnimationEnabled(true);
	    menu.addItem(new MenuItem("Calendar", calendarMenu));
	    
	    //add new client to DB
	    clientMenu.addItem("Add New Client", new Command(){
			public void execute() {

			}	
	    });
	    
	    //edit client details
	    clientMenu.addItem("Edit Current Client", new Command(){

			public void execute() {
				
			}
	    
	    });
	    
	    //remove client from DB
	    clientMenu.addItem("Remove Current Client", new Command(){

			public void execute() {
				
			}
	    	
	    });
	    
	    //create new project for current selected customer
	    projectsMenu.addItem("Create new project for current customer", new Command(){
			public void execute() {
				
			}
	    });
	    
	    //add new client task
	    calendarMenu.addItem("Add client task", new Command(){
			public void execute() {
				
			}
	    });
	    
	    //add establishment from last contact with a current selected client
	    calendarMenu.addItem("Add new contact details", new Command(){
			public void execute() {
				
			}	    	
	    });
	}
	
	private void initDetails() {
		ServiceDefTarget endPointClientDetails = (ServiceDefTarget) getClientDetailsService;
		endPointClientDetails.setServiceEntryPoint("pl.gwt.server");

		// get the phone number
		AsyncCallback<String> callbackPhoneNo = new AsyncCallback<String>() {
			public void onFailure(Throwable caught) {
				exist = false;
			}

			public void onSuccess(String result) {
				clientPhoneNoLabel.setText(result);
			}
		};

		// get the address
		AsyncCallback<String> callbackAddress = new AsyncCallback<String>() {
			public void onFailure(Throwable caught) {
				exist = false;
			}

			public void onSuccess(String result) {
				clientAddressLabel.setText(result);
			}
		};

		// get the country
		AsyncCallback<String> callbackCountry = new AsyncCallback<String>() {
			public void onFailure(Throwable caught) {
				exist = false;
			}

			public void onSuccess(String result) {
				clientCountryLabel.setText(result);
			}
		};

		// get the city
		AsyncCallback<String> callbackCity = new AsyncCallback<String>() {
			public void onFailure(Throwable caught) {
				exist = false;
			}

			public void onSuccess(String result) {
				clientCityLabel.setText(result);
			}
		};

		// get the state
		AsyncCallback<String> callbackState = new AsyncCallback<String>() {
			public void onFailure(Throwable caught) {
				exist = false;
			}

			public void onSuccess(String result) {
				clientStateLabel.setText(result);
			}
		};
		getClientDetailsService.getPhoneNo(clientName, callbackPhoneNo);
		getClientDetailsService.getAddress(clientName, callbackAddress);
		getClientDetailsService.getCountry(clientName, callbackCountry);
		getClientDetailsService.getCity(clientName, callbackCity);
		getClientDetailsService.getState(clientName, callbackState);
	}

}
*/


